package com.ruoyi.common.core.domain;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

import javax.persistence.Column;
import javax.persistence.MappedSuperclass;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Transient;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.EntityStatus;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.reflect.ReflectUtils;

@MappedSuperclass
public class BaseEntity implements Serializable {
	private static final long serialVersionUID = 1L;
	
	private static final Logger log = LoggerFactory.getLogger(BaseEntity.class);
	
	@PrePersist
    protected void prePersist() {
        if (StringUtils.isEmpty(createTime)) {
            createTime = DateUtils.dateTimeNow();
        }
        if (StringUtils.isEmpty(createBy) && SecurityUtils.getLoginUser() != null) {
        	createBy = SecurityUtils.getLoginUser().getUsername();
        }
    }

    @PreUpdate
    protected void preUpdate() {
        updateTime = DateUtils.dateTimeNow();
        if (SecurityUtils.getLoginUser() != null) {
        	updateBy = SecurityUtils.getLoginUser().getUsername();
        }
    }

	@Transient
	private String searchValue;

	@Column(nullable = true)
	private String createBy;

	@Column(nullable = true,columnDefinition="datetime")
	private String createTime;
	
	@Column(nullable = true)
	private String updateBy;
	
	@Column(nullable = true,columnDefinition="datetime")
	private String updateTime;
	
	@Column(nullable = true)
	private String remark;
	
	@Transient
	private Map<String, Object> params;
	
	@Transient
	private int entitystatus = EntityStatus.origin.getCode();// origin = 0,
																// updated = 1,
																// add = 2,
																// deleted = 3

	public int getEntitystatus() {
		return entitystatus;
	}

	public void setEntitystatus(int entitystatus) {
		this.entitystatus = entitystatus;
	}

	public String getSearchValue() {
		return searchValue;
	}

	public void setSearchValue(String searchValue) {
		this.searchValue = searchValue;
	}

	public String getCreateBy() {
		return createBy;
	}

	public void setCreateBy(String createBy) {
		this.createBy = createBy;
	}

	public String getCreateTime() {
		return createTime;
	}

	public void setCreateTime(String createTime) {
		this.createTime = createTime;
	}

	public String getUpdateBy() {
		return updateBy;
	}

	public void setUpdateBy(String updateBy) {
		this.updateBy = updateBy;
	}

	public String getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(String updateTime) {
		this.updateTime = updateTime;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public Map<String, Object> getParams() {
		if (params == null) {
			params = new HashMap<>();
		}
		return params;
	}

	public void setParams(Map<String, Object> params) {
		this.params = params;
	}
	
    public void setAttributeValue(String name, Object value) {
        if (name != null && value != null) {
            Field[] fields = this.getClass().getDeclaredFields();
            if (fields != null && fields.length > 0) {
                String voname = null;
                for (int i = 0; i < fields.length; i++) {
                    voname = fields[i].getName();
                    try {
                        // 全部小写判断
                        if (name.toLowerCase().equals(voname.toLowerCase())) {
                            voname = voname.replaceFirst(voname.substring(0, 1), voname.substring(0, 1).toUpperCase());
                            if (name.toLowerCase().indexOf("num")>-1){
                                ReflectUtils.invokeSetter(this, voname, Convert.toBigDecimal(value));
                            }else{
                                ReflectUtils.invokeSetter(this, voname, value);
                            }

                            return;
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        log.error(e.getMessage());
                    }
                }
            }
        }

    }


    public Object getAttributeValue(String fieldName) {
        return ReflectUtils.getFieldValue(this, fieldName);
    }

}
